package com.cssw.fyzb.core.dao.live;

import com.cssw.fyzb.core.model.dodb.CmfGift;
import com.cssw.fyzb.core.model.dodb.CmfLiveGift;
import com.cssw.fyzb.core.model.dodb.GiftLog;
import com.cssw.fyzb.core.model.dodb.User;
import com.cssw.fyzb.core.util.DateUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.Record;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Repository
public class GiftDaoImpl implements GiftDao {

    @Override
    public CmfGift queryById(Long giftId) {
        return CmfGift.dao.findById(giftId);
    }

    /**
     * 获取礼物列表
     *
     * @return
     */
    public Page<CmfGift> listGift(int pageNumber, int pageSize, Map param) {
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL(" select * from cmf_gift ");
        sqlParse.addSQL(" WHERE state !=0 ");
        sqlParse.addLikeSQL(" AND gift_name LIKE :giftName ");
        sqlParse.addSQL(" order by sort asc ");
        return CmfGift.dao.paginate(pageNumber, pageSize, sqlParse);
    }



    //添加礼物日志
    @Override
    public Long addSendGiftLog(Long fromUserId, Long toUserId, Long liveId, Long giftId, BigDecimal fCoin, BigDecimal yCoin) {
        CmfLiveGift cmfLiveGift = new CmfLiveGift();
        cmfLiveGift.setFromUserId(fromUserId);
        cmfLiveGift.setToUserId(toUserId);
        cmfLiveGift.setLiveId(giftId);
        cmfLiveGift.setGiftId(giftId);
        cmfLiveGift.setFCoin(fCoin);
        cmfLiveGift.setYCoin(yCoin);
        cmfLiveGift.setCreateTime(new Date());
        cmfLiveGift.save();
        return cmfLiveGift.getLgId();
    }

    @Override
    public Long addGiftLog(Long userId, Long giftId, String type, Integer nums, String tradeId, String tradeType, String tradeName) {
        GiftLog giftLog = new GiftLog();
        giftLog.setUserId(userId);
        giftLog.setGiftId(giftId);
        giftLog.setType(type);
        giftLog.setNums(nums);
        giftLog.setTradeId(tradeId);
        giftLog.setTradeType(tradeType);
        giftLog.setTradeName(tradeName);
        giftLog.setCreateTime(new Date());
        giftLog.save();
        return giftLog.getLogId();
    }

    @Override
    public Page<GiftLog> listGiftLog(Long userId, int pageNumber, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from t_gift_log where user_id=:userId ORDER BY create_time DESC");
        return GiftLog.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    /**
     * 主播收到礼物列表
     *
     * @return
     */
    public Page<CmfGift>  listReceivedGift(Long userId, int pageNumber, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select t2.* from cmf_live_gift t1 left join cmf_gift t2 on t1.gift_id=t2.gift_id where t1.to_user_id =:userId ");
        return CmfGift.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    /**
     * 用户赠送礼物列表
     *
     * @return
     */
    public Page<CmfGift>  listSendGift(Long userId, int pageNumber, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select t2.* from cmf_live_gift t1 left join cmf_gift t2 on t1.gift_id=t2.gift_id where t1.from_user_id =:userId ");
        return CmfGift.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    /**
     * 主播收到礼物统计(根据礼物id)
     *
     * @return
     */
    public Page<Record> receivedGiftStatistics(Long userId, int pageNumber, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("to_user_id", userId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select gift_id as giftId,count(*) as num from cmf_live_gift where to_user_id =:to_user_id GROUP BY gift_id ");
        return Db.paginate(pageNumber, pageSize, sqlParse);
    }

    /**
     * 管理员添加礼物
     */
    public Boolean addCmfGift(String type,Long cateId,String giftName,BigDecimal needCoin,BigDecimal giftYCoin,String iconMini,String image,int sort,Long sysOperId){
        CmfGift cmfGift = new CmfGift();
//        cmfGift.setType(type);
//        cmfGift.setCateId(cateId);
//        cmfGift.setGiftName(giftName);
//        cmfGift.setNeedCoin(needCoin);
//        cmfGift.setGiftYCoin(giftYCoin);
//        cmfGift.setIconMini(iconMini);
//        cmfGift.setImage(image);
//        cmfGift.setSort(sort);
//        cmfGift.setCreateOperId(sysOperId);
//        cmfGift.setCreateTime(new Date());
        return cmfGift.save();
    }

    /**
     * 修改礼物
     */
    public Boolean updateCmfGift(Long giftId,String type,Long cateId,String giftName,BigDecimal needCoin,BigDecimal giftYCoin,String iconMini,String image,int sort,Long sysOperId){
        CmfGift cmfGift = new CmfGift();
//        cmfGift.setGiftId(giftId);
//        cmfGift.setType(type);
//        cmfGift.setCateId(cateId);
//        cmfGift.setGiftName(giftName);
//        cmfGift.setNeedCoin(needCoin);
//        cmfGift.setGiftYCoin(giftYCoin);
//        if(iconMini != null)
//            cmfGift.setIconMini(iconMini);
//        if(image != null)
//            cmfGift.setImage(image);
//        cmfGift.setSort(sort);
//        cmfGift.setUpdateOperId(sysOperId);
//        cmfGift.setUpdateTime(new Date());
        return cmfGift.update();
    }

    /**
     * 删除礼物
     */
    public Boolean daleteCmfGift(Long giftId,Long sysOperId){
        CmfGift cmfGift = new CmfGift();
//        cmfGift.setGiftId(giftId);
//        cmfGift.setState("0");
//        cmfGift.setUpdateOperId(sysOperId);
//        cmfGift.setUpdateTime(new Date());
        return cmfGift.update();
    }

    public Page<CmfLiveGift> listCmfLiveGift(int pageNumber, int pageSize){
        Map<String, Object> params = new HashMap<>();
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("SELECT a.*,b.gift_name,b.image FROM cmf_live_gift a LEFT JOIN cmf_gift b ON a.gift_id=b.gift_id ");
        return CmfLiveGift.dao.paginate(pageNumber, pageSize, sqlParse);
    }
}
